package com.topisv.tms.web.forward;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.topisv.tms.entity.BackupTask;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.service.BackupTaskService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.web.dto.PrintEdiOrdeno;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/printBackupTask")
public class PrintBackupTaskController {
	private static final Logger log = LoggerFactory.getLogger(PrintBackupTaskController.class);
	
	@Autowired
	private BackupTaskService backupTaskService;
	@Autowired
	private CustomersService customersService;


	/**
	 * 订单打印
	 * @param backupTaskCode
	 * @param ordenos
	 * @param end
	 * @param response
	 */
	@RequestMapping(value = "ajaxPrintOrdeno")
	public void ajaxPrintOrdeno(@RequestParam(value = "backupTaskCode") String backupTaskCode,
			@RequestParam(value = "ordenos") String ordenos,
			@RequestParam(value = "end") String end,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json = null;
		List<PrintEdiOrdeno> printEdiOrdenos;
		Customers customers = null;
		try {
			out = response.getWriter();
			json = new JSONObject();
			BackupTask backupTask=backupTaskService.findByBackupTaskCode(backupTaskCode);
			customers=customersService.findByCustomerCode(backupTask.getCustomerCode());
			printEdiOrdenos = backupTaskService.getPrintEdiOrdeno(ordenos, customers, backupTaskCode);
			json.put("status", "1");
			json.put("content", printEdiOrdenos);
			json.put("end", end);
			json.put("count", printEdiOrdenos.size());
			out.print(json.toString());
		} catch (Exception e) {
			
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			log.error("打印出错了", e);
		}
	}

	/**
	 * 显示拣货列表
	 * 
	 * @param backupTaskCode
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "printInit/{backupTaskCode}")
	public String printInit(
			@PathVariable(value = "backupTaskCode") String backupTaskCode,
			Model model) {
		ShiroUser user = getShiroUser();
		BackupTask backupTask = null;
		List<EdiOrder> ediOrders = null;
		try {
			ediOrders = new ArrayList<EdiOrder>();
			ediOrders = backupTaskService.getEdiOrder(backupTaskCode,
					user.jobuuid);// 得到该拣货批次的订单信息
			model.addAttribute("ediOrders", ediOrders);
			backupTask = backupTaskService.findByBackupTaskCode(backupTaskCode);
			backupTask.setUsername(user.realname);
			model.addAttribute("obj", backupTask);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			
			return "redirect:/backupTask/backupTaskLists";
		}

		return "backupTask/printInit";
	}

	private ShiroUser getShiroUser() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user;
	}

}
